# -*- coding: utf8 -*-
import urllib2
import urllib
import re
import os
import time
def download(url,category):
    try:
        content=urllib2.urlopen(url).read()
    except:
        return()
    name=re.compile('<input value="(.*?)" id="doctitle"')
    time=re.compile('<input value="(.*?)" id="PAGETIME"')
    name_pat=re.findall(name,content)
    time_pat=re.findall(time,content)
    if name_pat==[]:
        return()
    if time_pat==[]:
        return()
    filename='N'+name_pat[0]+'T'+time_pat[0]+'.xml'
    filename=filename.replace(":","-")
    filename=filename.replace('/','~')
    filename="F:\Python27\Zgr03"+os.sep+category+os.sep+filename.decode('utf-8')
    outfile = open(filename,'w')
    outfile.write(content)
    outfile.close()
    temple=[]
    temple.append(re.findall(name_pat,content)[0])
    temple.append(re.findall(time_pat,content)[0])
    temple.append(filename)
    #print(temple)
    #print(temple[0])
    time.sleep(0.01)
    return (1)
#url=('http://www.baike.com/wiki/%E5%90%B8%E6%AF%92%E4%BA%9A%E6%96%87%E5%8C%96')
#category='\xe6\x96\x87\xe5\x8c\x96\xe7\x8e\xb0\xe8\xb1\xa1'+'T'
#category=category.decode('utf-8')
#download('http://tupian.baike.com/9139/1.html','x')
